#ifndef __SPRITE_3D_H__
#define __SPRITE_3D_H__

#include "cocos2d.h"

using namespace cocos2d;

const int MAX_INDICIES   = 3000;
const int MAX_TEXTURE_VERTICES   = 2000;
const int MAX_FRAMES   = 100;
const int MAX_NODES   = 10;
const int MAX_VERTICES   = 3000;

class Sprite3D : public CCSprite
{
	Sprite3D();
	virtual ~Sprite3D(void);


public:
	virtual void draw(void);
	static Sprite3D* spriteWithFile(const char *pszFileName, const char *meshFilename);
	virtual void update(ccTime delta);

	/** Try to add 3D function.
	*/
	bool initWith3DFile(const char *pszFilename, const char *meshFilename);


	unsigned char ReadUInt8( unsigned char ** m_pData )
	{
		char value = **m_pData; 
		++*m_pData;

		return value; 
	}

	unsigned short ReadUInt16( unsigned char ** m_pData ) 
	{
		short value = ( (*m_pData)[0] | ( (*m_pData)[1] << 8 ) );
		*m_pData += 2;

		return value; 
	}

	unsigned int ReadUInt32( unsigned char ** m_pData ) 
	{
		int value = ( (*m_pData)[0] | ( (*m_pData)[1] << 8 ) | ( (*m_pData)[2] << 16 ) | ( (*m_pData)[3] << 24 ));
		*m_pData += 4;

		return value; 
	}

	float ReadFloat( unsigned char ** m_pData )
	{
		int value = ( (*m_pData)[0] | ( (*m_pData)[1] << 8 ) | ( (*m_pData)[2] << 16 ) | ( (*m_pData)[3] << 24 ));
		*m_pData += 4;

		return *( (float *)( &value ) );
	}

	void ReadSkip( unsigned char ** m_pData, int count )
	{
		*m_pData += count;
	}

	void SetRange( int start, int end );
	void GetVerticesAt( int timeMS ) ;
	void GetInterpolationValues( int timeMS, int & frameA, int & frameB, float & interpolation ) const;
	void ComputeBounds( void );
	void BuildTweenFrame( int frameA, int frameB, float interpolation ) ;

	int m_IndexCount;
	int m_NodeCount;
	int m_FrameCount;
	int m_VertexCount;

	struct vec3
	{
		float x;
		float y;
		float z;
	};
	struct quaternion
	{
		float x;
		float y;
		float z;
		float w;
	};
	struct Node
	{
		vec3 position;
		quaternion rotation;
	};
	
	struct Frame
	{
		float vertices[MAX_VERTICES];
		Node nodes[MAX_NODES];
		int timeMS;
	};
	float m_TextureVertices[MAX_TEXTURE_VERTICES];
	short m_Indicies[MAX_INDICIES];
	Frame m_Frames[MAX_FRAMES];


	int m_TimeMS;
	int m_FrameStart;
	int m_FrameEnd;
	float m_BufferVertices[MAX_VERTICES];

	bool m_Looped;


	float m_NormalizedScale;
	vec3 m_Minimum;
	vec3 m_Maximum;
	vec3 m_Center;
	
};

#endif  // __SPRITE_3D_H__